package com.universal.frame.views;

import android.app.Activity;
import android.app.AlertDialog;
import android.view.Gravity;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.universal.frame.R;
import com.universal.frame.generalutils.SharedPreferencesUtil;

/**
 * 作者:RoreyDiu on 2015/12/24 15:15 自定义警示对话框
 */
public class CustomWarningDialog {

    private AlertDialog alertDialog;
    private Activity activity;
    private int windowWidth;

    public CustomWarningDialog(Activity activity) {
        this.activity = activity;
        this.windowWidth = SharedPreferencesUtil
                .getInt(activity, "windowWidth");
    }

    /**
     * 带确定和取消对话框并可以传递位置信息
     */
    public void showDialogAndSureOrCancel(String message, final int position,
                                          String sure, String cancle, final CustomWarningDialogInterface dialogAction) {
        if (alertDialog == null) {
            alertDialog = new AlertDialog.Builder(activity,
                    R.style.CustomWarningDialogStyle).create();
        }
        View view = View.inflate(activity,
                R.layout.customwarning_dialog_sureandcancel, null);
        alertDialog.show();
        LinearLayout ll_whole_dialog = (LinearLayout) view
                .findViewById(R.id.ll_whole_dialog);
        TextView tv_tip_message = (TextView) view
                .findViewById(R.id.tv_tip_message);
        TextView btn_ok = (TextView) view.findViewById(R.id.btn_ok);
        TextView btn_cancel = (TextView) view.findViewById(R.id.btn_cancel);
        if (sure != null) {
            btn_ok.setText(sure);
        }
        if (cancle != null) {
            btn_cancel.setText(cancle);
        }
        tv_tip_message.setText(message);
        // 动态获取视图的高度与宽度 其实也没有完全适配 textViewMessage的高度不是动态的 但是没有影响==================
        ll_whole_dialog.measure(0, 0);
        int ll_whole_dialog_height = ll_whole_dialog.getMeasuredHeight();
        // =============================================
        alertDialog.getWindow().setLayout(windowWidth * 77 / 100,
                (int) (ll_whole_dialog_height + tv_tip_message.getMeasuredHeight() * 2.5));//暂定2.5行高度
        alertDialog.getWindow().setGravity(Gravity.CENTER);
        alertDialog.setCanceledOnTouchOutside(false);
        alertDialog.setContentView(view);
        // =============================================================

        // 按钮监听
        btn_ok.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                if (dialogAction != null) {
                    dialogAction.sureAndCancleAction(position);
                }
                alertDialog.cancel();
            }
        });
        btn_cancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (dialogAction != null) {
                    dialogAction.cancelAction();
                }
                alertDialog.cancel();
            }
        });
    }

    /**
     * 只有确定按钮的对话框 自定义标题
     */
    public void showSureDialog(String message, String sure,
                               final CustomWarningDialogInterface dialogAction) {
        if (alertDialog == null) {
            alertDialog = new AlertDialog.Builder(activity,
                    R.style.CustomWarningDialogStyle).create();
        }
        View view = View.inflate(activity, R.layout.customwarning_dialog_sure,
                null);
        alertDialog.show();
        LinearLayout ll_whole_dialog = (LinearLayout) view
                .findViewById(R.id.ll_whole_dialog);
        TextView tv_tip_message = (TextView) view
                .findViewById(R.id.tv_tip_message);
        TextView btn_ok = (TextView) view.findViewById(R.id.btn_ok);
        if (sure != null) {
            btn_ok.setText(sure);
        }
        // ================================================
        ll_whole_dialog.measure(0, 0);
        int ll_whole_dialog_height = ll_whole_dialog.getMeasuredHeight();
        // 下面的写法不适配
        // alertDialog.getWindow().setLayout(windowWidth * 9 / 10, windowHeight
        // * 5 / 10);
        alertDialog.getWindow().setLayout(windowWidth * 77 / 100,
                (int) (ll_whole_dialog_height + tv_tip_message.getMeasuredHeight() * 2.5));
        alertDialog.getWindow().setGravity(Gravity.CENTER);
        alertDialog.setCanceledOnTouchOutside(false);
        alertDialog.setContentView(view);

        tv_tip_message.setText(message);
        // 按钮监听
        btn_ok.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                if (dialogAction != null) {
                    dialogAction.sureAction();
                }
                alertDialog.cancel();
            }
        });
    }

    public interface CustomWarningDialogInterface {

        public void sureAndCancleAction(int position);

        public void cancelAction();

        public void sureAction();

    }
}
